package cn.sinjet.phone;

import java.util.List;

import com.mgroup.wazehelper.R;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.sinjet.adapter.CallLogAdapter;
import cn.sinjet.carassist.MyActivity;
import cn.sinjet.viewmodel.PageDefine;
import cn.sinjet.viewmodel.ViewModel;

public class CallLogActivity extends MyActivity implements OnClickListener{
	private CallLogAdapter adapterContacts;  
    private ListView vContactList;  
//    private List<CallLog> allContacts = new ArrayList<CallLog>();
    List<CallLog> callLogs = null;//new ArrayList<CallLog>();
//    private QuickAlphabeticBar alphabeticBar; // 快速索引条  
    TextView vNumber;
    String strNumber = "";
   @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setMyContentView(R.layout.activity_phone_calllog);
        callLogs = PhoneModel.getIns(getApplicationContext()).getCalllogs();
		initView();
	}
   
   private void initView(){
	    vContactList = (ListView)mViewRoot.findViewById(R.id.phone_calllog_list);
		adapterContacts = new CallLogAdapter(callLogs, this);
		vContactList.setAdapter(adapterContacts);
		vContactList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int pos, long arg3) {
				// TODO Auto-generated method stub
				onContactItemClk(pos);
			}
		});
		PhoneModel.getIns(getApplicationContext()).setCalllogUpdated();
		vNumber = (TextView)mViewRoot.findViewById(R.id.dial_number);
		mViewRoot.findViewById(R.id.phone_contacts_tab).setOnClickListener(this);
   }
   
   private void onContactItemClk(int pos){
	   if(callLogs != null || pos < callLogs.size()){
	       CallLog callLog = callLogs.get(pos);
//	       contact.getNumber();
	       Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ callLog.getNumber()));
	       startActivity(intent);
	   }
   }
	
	private void setAdapter(List<CallLog> list) {
		Log.d("contact", "refresh calllog list");
		adapterContacts.setListItem(list);
		adapterContacts.notifyDataSetChanged();
//		adapter = new ContactListAdapter(this, list, alphabeticBar);
//		contactList.setAdapter(adapter);
//		alphabeticBar.init(ContactsActivity.this);
//		alphabeticBar.setListView(contactList);
//		alphabeticBar.setHight(alphabeticBar.getHeight());
//		alphabeticBar.setVisibility(View.VISIBLE)
		
	}
/*
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
	}*/
	
	
	private void onClkNum(String strNum){
    	if(strNumber.length()>=11)
    		return;
    	strNumber += strNum;
    	vNumber.setText(strNumber);
    	
//    	searchInAllContacts(strNumber);
//    	searchResult = ContactSearch.search(strNum, allContacts);
//    	setAdapter(searchResult);
    }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.phone_contacts_tab:
			ViewModel.getIns().startActivity(ContactsActivity.class);
			break;
		}
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ViewModel.getIns().onActivityResume(this, PageDefine.PAGE_PHONE_CALL_LOG, this);
		if(PhoneModel.getIns(getApplicationContext()).isCallogNeedUpdate())
			updateCalllogList();
	}
	
	@Override
	public void updateUI(Bundle data) {
		// TODO Auto-generated method stub
		super.updateUI(data);
		if(data.getInt("id",-1)==R.id.phone_calllog_list){
             updateCalllogList();			
		}
	}
	
	private void updateCalllogList(){
		Log.d("contact", "updateCalllogList");
		adapterContacts.setListItem(PhoneModel.getIns(getApplicationContext()).getCalllogs());
		adapterContacts.notifyDataSetChanged();
		PhoneModel.getIns(getApplicationContext()).setCalllogUpdated();
	}
}
	
